let that;
import {
  getHotSearch,
  getHistorySearch,
  delHistorySearch,
  newGetSearch,
} from "@/static/js/quiz.js";
import searchList from "./components/searchList/searchList.vue";
import { hightLightText, getUserAnalyze } from "@/utils/utils.js";
var loading = true;

export default {
  components: {
    searchList,
  },
  data() {
    return {
      isSearch: false, //是否显示搜索界面
      keyword: "",
      status: "more",
      page: 1,
      pagesize: 10,
      dataList: [],
      historyList: [],
      hotList: [],

      searchData: [],
      searchListShow: true,

      isGoKeyWord: true,

      count: 0,
      maxpage: 1,
      hightLightText: hightLightText,
      tagStyle: {
        em: "font-style: normal;color: #fa3534;",
      },
    };
  },
  onLoad(e) {
    this.keyword = e.keywords;
    if (
      this.keyword != "" &&
      this.keyword != null &&
      this.keyword != undefined
    ) {
      this.searchFun();
    }
    that = this;
    getUserAnalyze(2, 4, "/quiz/search/index", "AC7", "问答搜索");
  },
  onShow() {
    this.getHistorySearch();
    this.getHotSearch();
  },
  onReady() {},
  methods: {
    goBack() {
      uni.navigateBack({
        delta: 1,
      });
    },

    async changeInput(value) {
      if (value == "") {
        this.searchData = [];
        return;
      }
      if (!this.isGoKeyWord) {
        this.isGoKeyWord = true;
        return;
      }
      if (!loading) return;
      loading = false;
      const res = await newGetSearch({
        keyword: value,
        type: "xiaozhuwenda",
        page: 1,
        limit: 10,
      });
      loading = true;
      if (res.code == 1) {
        this.searchData = res.data.list;
      }
    },

    //清空
    delAll() {
      this.delHistorySearch();
      this.historyList = [];
    },

    //快速搜索
    fastSearch(item) {
      this.isGoKeyWord = false;
      this.keyword = item;
      this.searchFun();
    },
    //搜索
    searchFun() {
      let historyList = this.historyList;
      let keyword = this.keyword;
      this.page = 1;
      this.dataList = [];
      if (!keyword) {
        this.$tools.msg("请输入搜索内容");
      } else {
        if (historyList.indexOf(keyword) == -1) {
          historyList.unshift(keyword);
          if (historyList.length == 11) {
            historyList.pop(keyword);
          }
        }
        this.getDataList();
      }
    },
    //获取数据
    async getDataList() {
      this.searchData = [];
      this.searchListShow = false;

      this.isSearch = true;
      getUserAnalyze(2, 4, "/quiz/search/index", "AC7", this.keyword);
      let params = {
        keyword: this.keyword,
        type: "xiaozhuwenda",
        page: this.page,
        limit: this.pagesize,
      };
      let { code, data, msg } = await newGetSearch(params);

      this.searchData = [];
      this.searchListShow = true;

      if (code == 1) {
        for (let item of data.list) {
          item.title = hightLightText(item.title, this.keyword);
        }
        this.dataList.push(...data.list);
        this.count = data.count;
        this.maxpage = Math.ceil(data.count / this.pagesize);
      } else {
        that.$tools.msg(msg);
        this.status = "noMore";
      }
    },

    ReachBottom() {
      if (this.page < this.maxpage) {
        this.page++;
        this.getDataList();
      }
    },

    //获取问答历史搜索记录
    async getHistorySearch() {
      let res = await getHistorySearch();
      if (res.code == "1") {
        this.historyList = res.data;
      } else {
        this.$tools.msg(res.msg);
      }
    },

    //获取问答热门搜索
    async getHotSearch() {
      let that = this;
      let res = await getHotSearch();
      if (res.code == "1") {
        that.hotList = res.data;
      } else {
        that.$tools.msg(res.msg);
      }
    },
    //清空问答历史搜索记录
    async delHistorySearch() {
      let res = await delHistorySearch();
      if (res.code == "1") {
        this.historyList = [];
        that.$tools.msg("删除成功");
      }
    },
    //点击详情
    itemClick(item, index) {
      let url = `/quiz/askInfo/index?id=${item.id}`;
      this.$tools.href(url);
    },
  },
};